package Client端;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ClientDeal {
    private static boolean judge(String str) {
        if (str.equals("error")) return false;
        else if (str.equals("true")) return true;
        else if (str.equals("false")) return false;
        else return false;
    }

    private static String[][] split_2d(String str) {
        if ("".equals(str)) return new String[0][0];
        String[] s1 = str.split("&");
        String[][] s2 = new String[s1.length][];
        for (int i = 0; i < s1.length; i++) s2[i] = s1[i].split("~");
        return s2;
    }

    public static String changeDate(Date date) {
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static boolean checkAccount(String id, String password) {
        return judge(SocketClient.sendText("<login> " + id + "&" + password));
    }

    public static boolean addAccount(String id, String password, String username) {
        return judge(SocketClient.sendText("<register> " + id + "&" + password + "&" + username));
    }

    public static String getName(String id) {
        return SocketClient.sendText("<get> name " + id);
    }

    public static String[][] getFriend(String id) {
        //得到 id 的好友
        //返回值格式：String[朋友数][id username];
        return split_2d(SocketClient.sendText("<get> friend " + id));
    }

    public static boolean send(String id1, String id2, String text) {//发送信息
        return judge(SocketClient.sendText("<send> info " + id1 + "&" + id2 + "&" + text));
    }

    public static String[][] get(String id1, String id2, Date time1, Date time2) {//获得信息
        //返回值结构：String[第几个消息][发送方id 接收方id 信息]
        return split_2d(SocketClient.sendText(
                "<get> info " + id1 + "&" + id2 + "&" +
                        changeDate(time1) + "&" + changeDate(time2)));
    }

    public static String[][] getGroupInfo(String gid, Date time1, Date time2) {//获得群聊信息
        //返回值结构：String[第几个消息][发送方id 接收方id 信息]
        return split_2d(SocketClient.sendText("<get> info " + gid + "&" + changeDate(time1) + "&" + changeDate(time2)));
    }

    public static boolean addFriend(String id1, String id2) {
        //将id1和id2添加为好友
        if (id1.compareTo(id2) > 0) {
            String temp = id1;
            id1 = id2;
            id2 = temp;
        }
        return judge(SocketClient.sendText("<send> friend " + id1 + "&" + id2));
    }

    public static boolean addGroup(String uid/*个人id*/, String gid/*群id*/) {
        //将id添加到群聊里
        return judge(SocketClient.sendText("<send> group " + uid + "&" + gid));
    }

    public static String[][] getMember(String gid/*群id*/) {
        //通过群聊的id来返回群成员
        return split_2d(SocketClient.sendText("<get> group " + gid));
    }

    public static String[][] getGroup(String uid /*个人id*/) {
        //通过自己的id返回所在的群
        return split_2d(SocketClient.sendText("<get> in " + uid));
    }

    public static boolean createGroup(String name, String gid) {
        return judge(SocketClient.sendText("<create> " + name + "&" + gid));
    }
}
